package com.aolong.archives.common.resource;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.rock.platform.util.SessionUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class I18nBundleUtil {

	private static final I18nBundleUtil _instance = new I18nBundleUtil();    
	
	public static I18nBundleUtil getInstance() {    
	    return _instance;    
	}    
	
	private I18nBundleUtil()
	{
	}
	
	public void addResource(EntityManager em,String tableName,String languageFieldName,String keyFieldName,String valueFieldName)
	{
		StringBuffer strSql = new StringBuffer();
		strSql.append("select ");
		strSql.append(languageFieldName);
		strSql.append(", ");
		strSql.append(keyFieldName);
		strSql.append(", ");
		strSql.append(valueFieldName);
		strSql.append(" from ");
		strSql.append(tableName);
		Query query = em.createNativeQuery(strSql.toString());
        List<?> result = query.getResultList();
        this.addResource(result, 0, 1, 2);
	}
	public void addResource (ResultSet resultSet,String languageFieldName,String keyFieldName,String valueFieldName) throws SQLException 
	{
		int iLanguageIndex = 0,iKeyIndex = 1,iValueIndex = 2;
		ResultSetMetaData rsm =resultSet.getMetaData();
		int iCount = rsm.getColumnCount();
		for (int i = 0; i < iCount; i++) {
			if (languageFieldName.equals(rsm.getColumnName( i + 1 )))
			{
				iLanguageIndex = i;
			}
			if (keyFieldName.equals(rsm.getColumnName( i + 1 )))
			{
				iKeyIndex = i;
			}
			if (valueFieldName.equals(rsm.getColumnName( i + 1 )))
			{
				iValueIndex = i;
			}
		}
		this.addResource(resultSet, iLanguageIndex, iKeyIndex, iValueIndex);
	}
	public void addResource (ResultSet resultSet)  throws SQLException
	{
		this.addResource(resultSet, 0, 1, 2);
	}
	public void addResource (ResultSet resultSet,int languageFieldIndex,int keyFieldIndex,int valueFieldIndex) throws SQLException 
	{
		if (resultSet == null) return;
		resultSet.first();
		while(resultSet.next()) { 
			String language = resultSet.getString(languageFieldIndex);
			Map<String,Object> map = lookup.get(language);
			if (map == null)
			{
				map = new HashMap<String,Object>();
				lookup.put(language, map);
			}
			map.put(resultSet.getString(keyFieldIndex),resultSet.getObject(valueFieldIndex));
		}
	}
	public void addResource (List<? extends Object> queryList) 
	{
		this.addResource(queryList, 0, 1, 2);
	}
	public void addResource (List<? extends Object> queryList,int languageIndex,int keyIndex,int valueIndex) 
	{
        if (queryList == null) return;

        for(int i = 0; i < queryList.size(); i++)
		{
			Object[] objAry = (Object[])queryList.get(i);
			String language = (String)objAry[languageIndex];
			Map<String,Object> map = lookup.get(language);
			if (map == null)
			{
				map = new HashMap<String,Object>();
				lookup.put(language, map);
			}
			map.put((String)objAry[keyIndex],objAry[valueIndex]);
		}
    }
	
	public void clearResource()
	{
		lookup.clear();
	}
	
	private Map<String,Object> getLookup(String language)
	{
		Map<String,Object> _lookup = lookup.get(language);
		return (_lookup == null) ? new HashMap<String,Object>() : _lookup;
	}

	public String getString(String key,String language) {
        if (key == null) {
            return "";
        }
        Map<String,Object> _lookup = getLookup(language);
        
        if (!_lookup.containsKey(key))
        {
        	return key;
        }
        else
        {
        	return _lookup.get(key).toString();
        }
	}
	public String getString(String key) {
        String language = SessionUtils.getAttributeText(SessionUtils.SKEY_LANGUAGE);
		return getString(key,language);
	}
	public String getJsonString(String language)
	{
		JSONArray ja = new JSONArray();
		Set<String> keys = getLookup(language).keySet();
		for(String key : keys)
		{
			JSONObject jo = new JSONObject();
			jo.put("key", key);
			jo.put("text", this.getString(key,language));
			ja.add(jo);
		}
		return ja.toString();
	}

	
    public static String getI18n(String key)
    {
    	return I18nBundleUtil.getInstance().getString(key);
    }
    public static String getI18n(String key,String language)
    {
    	return I18nBundleUtil.getInstance().getString(key,language);
    }
    
    private Map<String,Map<String,Object>> lookup = new HashMap<String,Map<String,Object>>() ;
}
